
package com.yuan.control;

import com.yuan.localServer.LocalManage;
import com.yuan.model.M上班考勤表;
import com.yuan.model.M下班考勤表;
import com.yuan.model.M员工信息表;
import com.yuan.model.M员工活动轨迹表;
import com.yuan.model.M考勤表;
import com.yuan.model.Users;
import com.yuan.prefence.FileConstants;
import com.yuan.prefence.PreferenceKeys;
import com.yuan.unit.AsyncDataJobHandler;
import com.yuan.unit.FileUtils;
import com.yuan.unit.PreferenceUtil;
import com.yuan.unit.TimeUtils;
import com.yuan.unit.VibratorUtils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Handler;

import java.io.IOException;
import java.io.StreamCorruptedException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * 签到控制类
 * 
 * @author YUAN
 */
@SuppressLint("SimpleDateFormat")
public class CheckControl implements PreferenceKeys {

    /**
     * 上班签到管理
     */
    public static void setCheckIn(Context mContext, Handler mHandler)
    {
        String str = LocalManage.getInstance().adress;
        double lng = LocalManage.getInstance().lng;
        double lat = LocalManage.getInstance().lat;
        M上班考勤表.getInstance().set位置(str);
        M上班考勤表.getInstance().set日期(TimeUtils.getNowTime());
        M上班考勤表.getInstance().set纬度(lat);
        M上班考勤表.getInstance().set经度(lng);
        if (SettingControl.IsVolice())
        {
            SoundControl.getInstance(mContext).playSounds(0, 1);
        }
        if (SettingControl.IsShare())
        {
            VibratorUtils.Vibrate(mContext, SettingControl.SHARETIME);
        }
        AsyncDataJobHandler.getInstance().checkIn(mHandler);
    }

    public static void setCheckOut(Context mContext, Handler mHandler)
    {
        String str = LocalManage.getInstance().adress;
        double lng = LocalManage.getInstance().lng;
        double lat = LocalManage.getInstance().lat;
        M下班考勤表.getInstance().set位置(str);
        M下班考勤表.getInstance().set日期(TimeUtils.getNowTime());
        M下班考勤表.getInstance().set纬度(lat);
        M下班考勤表.getInstance().set经度(lng);
        if (SettingControl.IsVolice())
        {
            SoundControl.getInstance(mContext).playSounds(0, 1);
        }
        if (SettingControl.IsShare())
        {
            VibratorUtils.Vibrate((Activity) mContext, SettingControl.SHARETIME);
        }
        AsyncDataJobHandler.getInstance().checkOut(mHandler);
    }

    public static boolean isCheckedInToday()
    {
        return PreferenceUtil.getInstance().getBoolean(IS_CHECKIN_TODAY, false);
    }

    public static void setCheckedInToday(boolean flag) {

        PreferenceUtil.getInstance().putBoolean(IS_CHECKIN_TODAY, flag);
    }

    public static boolean isCheckedOutToday()
    {
        return PreferenceUtil.getInstance().getBoolean(IS_CHECKOUT_TODAY, false);
    }

    public static void setCheckedOutToday(boolean flag) {

        PreferenceUtil.getInstance().putBoolean(IS_CHECKOUT_TODAY, flag);
    }

    public static boolean isRightStartWorkTime(String str)
    {
        if (str.trim().length() != 5)
            return false;
        SimpleDateFormat formatDate = new SimpleDateFormat("HH:mm");
        try {
            @SuppressWarnings("unused")
            Date startWorkTime = formatDate.parse(str);
        } catch (ParseException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
            return false;
        }
        return true;
    }

    public static boolean isLeaveRightTime(String str)
    {
        SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            @SuppressWarnings("unused")
            Date startWorkTime = formatDate.parse(str);
        } catch (ParseException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
            return false;
        }
        return true;
    }

    public static Date getStartWorkTime()
    {
        Date startWorkTime = null;
        try {
            String StartWorkStr = null;
            StartWorkStr = PreferenceUtil.getInstance().getString(END_WORK_TIME, null);
            SimpleDateFormat formatDate = new SimpleDateFormat("HH:mm");
            startWorkTime = formatDate.parse(StartWorkStr);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
        return startWorkTime;
    }

    /**
     * 是否大于上班时间 小于上班时间返回true 否则返回false
     * 
     * @param str
     * @return
     */
    public static boolean isRightChechinTime(String str) {
        str = str.substring(10, 16);
        String rightTime = SettingControl.getStartWorkTime();
        SimpleDateFormat df = new SimpleDateFormat("hh:mm");
        try {
            Date dt1 = df.parse(str);
            Date dt2 = df.parse(rightTime);
            if (dt1.compareTo(dt2) >= 0) {
                return false;
            }
        } catch (ParseException exception) {
            exception.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 是否是正确的下班时间 如果大于设定时间 则返回true否则返回false
     * 
     * @param str
     * @return
     */
    public static boolean isRightChechoutTime(String str) {
        str = str.substring(10, 16);
        String rightTime = SettingControl.getEndWorkTime();
        SimpleDateFormat df = new SimpleDateFormat("hh:mm");
        try {
            Date dt1 = df.parse(str);
            Date dt2 = df.parse(rightTime);
            if (dt1.compareTo(dt2) <= 0) {
                return false;
            }
        } catch (ParseException exception) {
            exception.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 是否大于下班时间 小于下班时间返回false 否则返回true
     * 
     * @param str
     * @return
     */
    public static void init上班M考勤表() {
        try {
            M上班考勤表.getInstance().setFromObject(
                    FileUtils.File2erializable(FileConstants.SERIALIZABLE_上班考勤));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void init下班M考勤表() {
        // TODO Auto-generated method stub
        try {
            M下班考勤表.getInstance().setFromObject(
                    FileUtils.File2erializable(FileConstants.SERIALIZABLE_下班考勤));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void init请假申请() {
        // TODO Auto-generated method stub

    }

    /**
     * 初始化员工信息表 并对其他的model进行初始化
     * 
     * @param mContext
     */
    public static void init员工信息表(Context mContext) {
        // TODO Auto-generated method stub
        try {
            M员工信息表.getInstance().setFromObject(
                    FileUtils.File2erializable(FileConstants.SERIALIZABLE_员工信息表));
            M考勤表.getInstance().set序号(M员工信息表.getInstance().get序号() + "");
            M员工活动轨迹表.getInstance().set序号(M员工信息表.getInstance().get序号());
            M上班考勤表.getInstance().set序号(M员工信息表.getInstance().get序号() + "");
            M下班考勤表.getInstance().set序号(M员工信息表.getInstance().get序号() + "");
            M上班考勤表.getInstance().isCheckedFromNet(mContext);
            M下班考勤表.getInstance().isCheckedFromNet(mContext);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void initUser() {
        // TODO Auto-generated method stub
        try {
            Users.getInstance().setFromMap(
                    (Map<?, ?>) FileUtils.File2erializable(FileConstants.SERIALIZABLE_USERS));
            M员工信息表.getInstance().set电话(Users.getInstance().get电话());
        } catch (StreamCorruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
